package com.app.controller;

import com.app.model.TrainModel;
import com.app.service.TrainService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.TrainVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//培训机构信息
@RestController
@Controller
@RequestMapping("/jiuquan/train")
public class TrainController {
    @Autowired
    private TrainService trainService;

    //查询列表
    @GetMapping("/getTrainList")
    public Result getTrainList(@RequestParam(defaultValue = "0") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit, String keyword, Integer cityId) {
        TrainVo trainList = trainService.getTrainList(page, limit, keyword, cityId);
        return Result.success(trainList);
    }

    //详情
    @GetMapping("/getTrainById")
    public Result getTrainById(Integer id) {
        TrainModel train = trainService.getById(id);
        return Result.success(train);
    }

    //新增和修改
    @PostMapping("/addAndUpdateTrain")
    public Result addAndUpdateTrain(@RequestBody TrainModel trainModel) {
        TrainModel train = trainService.getById(trainModel.getId());
        if (train != null) {
            trainModel.setUpdateTime(CommonHelp.getNowTimestamp());
            trainService.updateById(trainModel);
            return Result.success("修改成功");
        } else {
            trainModel.setCreateTime(CommonHelp.getNowTimestamp());
            trainService.save(trainModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delTrain")
    public Result delTrain(Integer id) {
        trainService.removeById(id);
        return Result.success("删除成功");
    }

    //用户查询列表
    @GetMapping("/getUserTrainList")
    public Result getUserTrainList(@RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer limit, String keyword, Double longitude, Double latitude, Integer city,Integer type) {
        TrainVo trainList = trainService.getUserTrainList(page, limit, keyword, longitude, latitude, city,type);
        return Result.success(trainList);
    }

    //用户查询详情
    @GetMapping("/getUserTrainById")
    public Result getUserTrainById(Integer id, Double longitude, Double latitude) {
        TrainModel train = trainService.getUserTrainById(id, longitude, latitude);
        return Result.success(train);
    }
}
